// pages/poster/resume/resume.js
import * as zrender from '../zrender/zrender';
import * as zrhelper from '../zrender/zrender-helper';
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
let zr;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reAvatorTem: '',
    reedAvatorTem: '',
    miniCodeTemp: '',
    recommendClassifyId: 0,
    rewardId: 0,
    url: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let {
      reAvator,
      reName,
      reedAvator,
      reedName,
      reedSex,
      reedAge,
      reedEdu,
      reedWork,
      reedPos,
      reedSalary,
      recommendClassifyId, 
      rewardId,
      url
    } = options;

    console.log()
    this.data.reAvator = decodeURIComponent(reAvator);
    this.data.reName = reName;
    this.data.reedAvator = decodeURIComponent(reedAvator);
    this.data.reedName = reedName;
    this.data.reedSex = reedSex;
    this.data.reedAge = reedAge;
    this.data.reedWork = reedWork;
    this.data.reedEdu = reedEdu;
    this.data.reedPos = reedPos;
    this.data.reedSalary = reedSalary;
    this.data.recommendClassifyId = recommendClassifyId;
    this.data.rewardId = rewardId;
    this.data.url = url;

    this.getReommendAvator();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 推荐人头像
   */
  getReommendAvator() {
    wx.showLoading({
      title: '生成中',
    });
    new Promise((resolve, rejet) => {
      wx.getImageInfo({
        src: this.data.reAvator,
        success: (res) => {
          resolve(res);
        }
      })
    }).then((res) => {
      this.data.reAvatorTem = res.path;
      this.getReCommendedAvator();
    })
  },

  /**
   * 被推荐人头像
   */
  getReCommendedAvator() {
    new Promise((resolve, rejet) => {
      wx.getImageInfo({
        src: this.data.reedAvator,
        success: (res) => {
          resolve(res);
        }
      })
    }).then((res) => {
      this.data.reedAvatorTem = res.path;
      this.getMinicode();
    })
  },

  /**
   * 小程序码-招聘端的
   */
  getMinicode() {
    let param = {
      scene: 'id=' + this.data.rewardId + '&recommendClassifyId=' + recommendClassifyId + '&isShare=true',
      page: this.data.url,
      width: 127
    }
    wx.downloadFile({
      url: config.api + 'mini_code?' + T.param(param),
      success: (res) => {
        wx.getImageInfo({
          src: res.tempFilePath,
          success: (_res) => {
            console.log(_res.path)
            this.data.miniCodeTemp = _res.path;
            // 绘画
            this.renderCanvas();
          },
          fail: (err) => {
            wx.hideLoading();
            wx.showToast({
              title: err.errMsg,
              icon: 'none'
            })
          }
        })
      }
    })
  },

  zrAddImage(x, y, url, w, h) {
    zr.add(new zrender.Image({
      style: {
        x: x,
        y: y,
        image: url,
        width: w,
        height: h
      }
    }));
  },

  zrAddText(x, y, text, textFont) {
    zr.add(new zrender.Text({
      style: {
        x: x,
        y: y,
        text: text,
        textFill: '#fff',
        textFont: textFont || '12px Microsoft Yahei',
      }
    }));
  },

  zrAddCircleImg(x, y, url, w, h, cx, cy, r) {
    let recomAvator = new zrender.Image({
      style: {
        x: x,
        y: y,
        image: url,
        width: w,
        height: h
      }
    });
    let circle = new zrender.Circle({
      shape: {
        cx: cx,
        cy: cy,
        r: r
      }
    });
    recomAvator.setClipPath(circle);
    zr.add(recomAvator);
  },

  zrAddLine(x1, y1, x2, y2) {
    zr.add(new zrender.Line({
      shape: {
        x1: x1,
        y1: y1,
        x2: x2,
        y2: y2
      },
      style: {
        lineWidth: 2,
        stroke: '#fff',
      }
    }));
  },

  renderCanvas() {
    zr = zrhelper.createZrender('canvas');
    // 上部背景图
    this.zrAddImage(0, 0, '../poster_blue_bg.png', 375, 480);

    // 推荐用户-头像
    this.zrAddCircleImg(79, 40, this.data.reAvatorTem, 35, 35, 96.5, 57.5, 17.5);

    // 文案说明-顶部
    this.zrAddText(121, 41, this.data.reName + '向您推荐了一份简历,')
    this.zrAddText(121, 58, '赶快扫描下方小程序码看一看吧~')


    // 被推荐人的头像
    this.zrAddCircleImg(148, 115, this.data.reedAvatorTem, 80, 80, 188, 155, 40);

    // 被推荐人的名字
    this.zrAddText(167, 210, this.data.reedName, '20px Microsoft Yahei');

    // 性别
    this.zrAddText(88, 243, this.data.reedSex)
    this.zrAddLine(110, 241, 110, 255);

    // 年龄
    this.zrAddText(120, 243, this.data.reedAge);
    this.zrAddLine(154, 241, 154, 255);

    // 学历
    this.zrAddText(170, 243, this.data.reedEdu);
    this.zrAddLine(198, 241, 198, 255);

    // 工作经验
    this.zrAddText(210, 243, this.data.reedWork);

    // 期望职位
    this.zrAddText(167, 280, '期望职位', '10px Microsoft Yahei');

    // 职位名
    zr.add(new zrender.Text({
      style: {
        x: 187.5,
        y: 296,
        text: this.data.reedPos,
        textFill: '#fff',
        textFont: '20px Microsoft Yahei',
        textAlign: 'center'
      }
    }));

    // 期望薪资
    this.zrAddText(167, 342, '期望薪资', '10px Microsoft Yahei');
    zr.add(new zrender.Text({
      style: {
        x: 187.5,
        y: 360,
        text: this.data.reedSalary,
        textFill: '#fff',
        textFont: '24px Microsoft Yahei',
        textAlign: 'center'
      }
    }));
    
    let rect = new zrender.Rect({
      style: {
        fill: '#fff',
      },
      shape: {
        x: 0,
        y: 480,
        width: 375,
        height: 187
      }
    });
    zr.add(rect);
    // 小程序码
    this.zrAddImage(124, 502, this.data.miniCodeTemp, 127, 127);

    zr.on('rendered', () => {
      wx.hideLoading();
      setTimeout(() => {
        wx.canvasToTempFilePath({
          canvasId: 'canvas',
          quality: 1,
          fileType: 'jpg',
          success: (res) => {
            this.setData({
              postImg: res.tempFilePath
            });
          }
        })
      }, 500);
    })
  },

  onKeepPoster() {
    wx.showLoading({
      title: '保存中...',
    })
    setTimeout(() => {
      wx.canvasToTempFilePath({
        canvasId: 'canvas',
        quality: 1,
        fileType: 'jpg',
        success: (res) => {
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: () => {
              wx.hideLoading();
              T.toast({
                id: '#toast',
                text: "已经保存到手机照片"
              });
            },
            fail: (err) => {
              console.log(err);
            }
          })
        }
      })
    }, 0.5 * 1000);
  }
})